package org.subject.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.subject.dao.RegistrationMapper;
import org.subject.dto.RegisteMessage;
import org.subject.dto.RegisteUserMessage;
import org.subject.entity.Message;
import org.subject.entity.Registration;
import org.subject.entity.User;
import org.subject.service.RegistrationService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 竞赛报名相关信息
 * Created by kai on 2017/5/6.
 */
@Service
public class RegistrationServiceImpl implements RegistrationService {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RegistrationMapper registrationMapper;

    /**
     * 获取报名用户列表
     *
     * @param messageId
     * @return
     */
    public List<User> selectUserList(String messageId) {
        return registrationMapper.selectUserList(Long.valueOf(messageId));
    }

    /**
     * 插入竞赛报名信息
     *
     * @param messageId
     * @param userId
     * @return
     */
    public int insert(String messageId, String userId) {
        Registration registration = new Registration();
        registration.setMessageId(Long.valueOf(messageId));
        registration.setUserId(Long.valueOf(userId));
        registration.setRegistrationState("1");
        return registrationMapper.insertSelective(registration);
    }

    /**
     * 根据消息id和用户id查询
     *
     * @param messageId
     * @param userId
     * @return
     */
    public Registration selectByMIdUid(String messageId, String userId) {
        Registration registration = new Registration();
        registration.setMessageId(Long.valueOf(messageId));
        registration.setUserId(Long.valueOf(userId));
        registration.setRegistrationState("1");
        return registrationMapper.selectByMIdUid(registration);
    }

    /**
     * 查询所有报名竞赛相关信息
     *
     * @param messageId
     * @return
     */
    public List<RegisteMessage> selectRegisteMessage(String messageId) {
        return registrationMapper.selectRegisteMessage(Long.valueOf(messageId));
    }

    /**
     * 根据userId查找相关信息
     *
     * @param userId
     * @return
     */
    public List<RegisteUserMessage> selectRegisteUserMessage(String userId) {
        return registrationMapper.selectRegisteUserMessage(Long.valueOf(userId));
    }

    public Registration selectByPrimaryKey(String registrationId) {
        return registrationMapper.selectByPrimaryKey(Long.valueOf(registrationId));
    }

    public int updateByPrimaryKey(Registration registration) {
        return registrationMapper.updateByPrimaryKeySelective(registration);
    }

    public List<User> selectUserListByFId(String messageId, String facultyId) {
        Message message = new Message();
        if (messageId != null) {
            message.setMessageId(Long.valueOf(messageId));
        }
        User user = new User();
        if (facultyId != null) {
            user.setFacultyId(Long.valueOf(facultyId));
        }
        Map<String, Object> paramter = new HashMap<String, Object>();
        paramter.put("message", message);
        paramter.put("user", user);
        return registrationMapper.selectUserListByFId(paramter);
    }
}
